﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;


public partial class TestInfo : System.Web.UI.Page
{
    DataSet dsTest = new DataSet();
    //static object userId;
    //public static int testId;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //userId = Session["UserId"];
            //testId = Convert.ToInt32(Request.QueryString["TestId"]);
            dsTest = AdminCreateTestDAL.GetTestInfoByTestId(Convert.ToInt32(Request.QueryString["TestId"]));
            DateTime expiry = Convert.ToDateTime(dsTest.Tables[0].Rows[0][6].ToString());
            if (DateTime.Now > expiry)
                btnTakeTest.Enabled = false;
            else
                btnTakeTest.Enabled = true;
            dsTest.Tables[0].Columns.Add("TopicName", typeof(System.String));
            setValues();
        }
    }

    private void setValues()
    {
        lblTestName.Text = dsTest.Tables[0].Rows[0][1].ToString();
        lblSubject.Text = dsTest.Tables[0].Rows[0][2].ToString();
        bindTopics();
        lblTopic.Text = dsTest.Tables[0].Rows[0]["TopicName"].ToString();
        lblQuestions.Text = dsTest.Tables[0].Rows[0][3].ToString();
        lblCutOffMarks.Text = dsTest.Tables[0].Rows[0][4].ToString();
        lblDuration.Text = dsTest.Tables[0].Rows[0][5].ToString();
        lblExpiresOn.Text = dsTest.Tables[0].Rows[0][6].ToString();
        bindGrid();
        int maxAttempt=Int32.MaxValue;
        DataTable dtMaxAtt = AdminCreateTestDAL.GetMaxAttempt(Convert.ToInt32(Request.QueryString["TestId"]));
        if(dtMaxAtt.Rows.Count>0)
        maxAttempt = Convert.ToInt32(dtMaxAtt.Rows[0][0]);
        DataTable dtTestAttempt = new DataTable();
        dtTestAttempt = AdminCreateTestDAL.GetTestAttemptInfo(Session["UserId"],Convert.ToInt32(Request.QueryString["TestId"]));
        if (dtTestAttempt.Rows.Count > 0)
        {
            int testAttNo = Convert.ToInt32(dtTestAttempt.Rows[dtTestAttempt.Rows.Count - 1][0]);
            if (testAttNo >= maxAttempt)
                btnTakeTest.Enabled = false;
        }
    }

    private void bindGrid()
    {
        DataTable dtTest = new DataTable();
        dtTest = AdminCreateTestDAL.GetTestAttemptAllInfo(Session["UserId"],Convert.ToInt32(Request.QueryString["TestId"]));
        gvTestAttempt.DataSource = dtTest;
        gvTestAttempt.DataBind();
    }

    private void bindTopics()
    {
        int i, j;
        string topics;

        for (i = 0; i < dsTest.Tables[0].Rows.Count; i++)
        {
            for (j = 0; j < dsTest.Tables[1].Rows.Count; j++)
            {
                if (dsTest.Tables[0].Rows[i][0].ToString() == dsTest.Tables[1].Rows[j][0].ToString())
                    dsTest.Tables[0].Rows[i]["TopicName"] += dsTest.Tables[1].Rows[j][1] + ",";
            }
        }

        for (i = 0; i < dsTest.Tables[0].Rows.Count; i++)
        {
            topics = dsTest.Tables[0].Rows[i]["TopicName"].ToString();
            topics = topics.Substring(0, topics.Length - 1);
            dsTest.Tables[0].Rows[i]["TopicName"] = topics;
        }
    }

    protected void btnTakeTest_Click(object sender, EventArgs e)
    {

        Response.Redirect("~/Student/TestInProgress.aspx?TestId=" + Convert.ToInt32(Request.QueryString["TestId"]) );
    }

    protected void gvTestAttempt_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvTestAttempt.PageIndex = e.NewPageIndex;
        bindGrid();
    }
    protected void gvTestAttempt_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int attId = Convert.ToInt32(e.CommandArgument);

        switch (e.CommandName)
        {
            case "btnScoreClick": Response.Redirect("~/Student/Score.aspx?AttemptId=" + attId);
                break;
        }  
    }
}
